//订单管理
#include "tab3.h"

// 订单管理相关变量
static lv_obj_t *orders_table = NULL;
static lv_obj_t *delete_order_confirm_modal = NULL;
static User *order_user_to_delete = NULL;
static int order_index_to_delete = -1;

// 刷新订单表格
static void refresh_orders_table(void)
{
    if (!orders_table || !userList) return;
    
    // 清除现有内容
    lv_table_set_row_cnt(orders_table, 1); // 保留标题行
    lv_table_set_cell_value(orders_table, 0, 0, "用户名");
    lv_table_set_cell_value(orders_table, 0, 1, "车次");
    lv_table_set_cell_value(orders_table, 0, 2, "出发站");
    lv_table_set_cell_value(orders_table, 0, 3, "到达站");
    lv_table_set_cell_value(orders_table, 0, 4, "日期");
    lv_table_set_cell_value(orders_table, 0, 5, "座位号");
    lv_table_set_cell_value(orders_table, 0, 6, "操作");
    
    // 重新加载用户数据
    freeUserList(userList);
    userList = initUserList();
    if (userList) 
    {
        loadUsersFromFile(userList, "userManagement.json");
    }

    int row = 1;
    User *current_user = userList->head;
    
    // 遍历所有用户和他们的订单
    while (current_user) 
    {
        for (int i = 0; i < current_user->orderCount; i++) 
        {
            lv_table_set_row_cnt(orders_table, row + 1);
            
            // 用户名
            lv_table_set_cell_value(orders_table, row, 0, current_user->username);
            
            // 车次信息
            lv_table_set_cell_value(orders_table, row, 1, current_user->orders[i].TrainNumber);
            lv_table_set_cell_value(orders_table, row, 2, current_user->orders[i].start);
            lv_table_set_cell_value(orders_table, row, 3, current_user->orders[i].end);
            lv_table_set_cell_value(orders_table, row, 4, current_user->orders[i].date);
            
            // 座位号
            char seat[10];
            snprintf(seat, sizeof(seat), "%d", current_user->orders[i].seatNumber);
            lv_table_set_cell_value(orders_table, row, 5, seat);
            
            // 删除按钮
            lv_table_set_cell_value(orders_table, row, 6, "删除");
            
            row++;
        }
        current_user = current_user->next;
    }
}

// 关闭删除确认模态窗口
static void close_order_delete_confirm_modal_cb(lv_event_t *e) 
{
    if (delete_order_confirm_modal) 
    {
        lv_obj_del(delete_order_confirm_modal);
        delete_order_confirm_modal = NULL;
        order_user_to_delete = NULL;
        order_index_to_delete = -1;
    }
}

// 确认删除订单
static void confirm_delete_order_cb(lv_event_t *e) 
{
    if (!order_user_to_delete || order_index_to_delete == -1) 
    {
        close_order_delete_confirm_modal_cb(e);
        return;
    }
    
    // 从用户订单数组中删除订单
    if (order_index_to_delete >= 0 && order_index_to_delete < order_user_to_delete->orderCount) 
    {
        // 将后面的订单向前移动
        for (int i = order_index_to_delete; i < order_user_to_delete->orderCount - 1; i++) 
        {
            order_user_to_delete->orders[i] = order_user_to_delete->orders[i + 1];
        }
        
        // 减少订单计数
        order_user_to_delete->orderCount--;
        
        // 保存到文件
        saveUsersToFile(userList, "userManagement.json");
        
        // 刷新表格
        refresh_orders_table();
        
        printf("订单已删除\n");
    } 
    else 
    {
        printf("删除订单失败\n");
    }
    
    close_order_delete_confirm_modal_cb(e);
}

// 显示删除确认对话框
static void show_order_delete_confirm_dialog(User *user, int order_index) 
{
    if (!user || order_index < 0 || order_index >= user->orderCount) return;
    
    order_user_to_delete = user;
    order_index_to_delete = order_index;
    
    // 创建确认模态窗口
    delete_order_confirm_modal = lv_obj_create(lv_scr_act());
    lv_obj_set_size(delete_order_confirm_modal, 350, 200);
    lv_obj_align(delete_order_confirm_modal, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_opa(delete_order_confirm_modal, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(delete_order_confirm_modal, lv_color_hex(0x2D2D3A), 0);
    lv_obj_set_style_border_width(delete_order_confirm_modal, 2, 0);
    lv_obj_set_style_border_color(delete_order_confirm_modal, lv_color_white(), 0);
    
    // 确认消息
    lv_obj_t *confirm_label = lv_label_create(delete_order_confirm_modal);
    char confirm_text[150];
    snprintf(confirm_text, sizeof(confirm_text), "确定要删除用户 %s 的\n%s 次列车订单吗？", 
             user->username, user->orders[order_index].TrainNumber);
    lv_label_set_text(confirm_label, confirm_text);
    lv_obj_set_style_text_font(confirm_label, font_chinese, 0);
    lv_obj_set_style_text_color(confirm_label, lv_color_white(), 0);
    lv_obj_align(confirm_label, LV_ALIGN_TOP_MID, 0, 30);
    
    // 按钮容器
    lv_obj_t *btn_container = lv_obj_create(delete_order_confirm_modal);
    lv_obj_set_size(btn_container, 300, 50);
    lv_obj_align(btn_container, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_set_flex_flow(btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_bg_opa(btn_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);
    
    // 确认按钮
    lv_obj_t *confirm_btn = lv_btn_create(btn_container);
    lv_obj_set_size(confirm_btn, 100, 40);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0xF44336), 0); // 红色
    lv_obj_t *confirm_btn_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_btn_label, "确定");
    lv_obj_set_style_text_font(confirm_btn_label, font_chinese, 0);
    lv_obj_center(confirm_btn_label);
    lv_obj_add_event_cb(confirm_btn, confirm_delete_order_cb, LV_EVENT_CLICKED, NULL);
    
    // 取消按钮
    lv_obj_t *cancel_btn = lv_btn_create(btn_container);
    lv_obj_set_size(cancel_btn, 100, 40);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(0x9E9E9E), 0); // 灰色
    lv_obj_t *cancel_btn_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_btn_label, "取消");
    lv_obj_set_style_text_font(cancel_btn_label, font_chinese, 0);
    lv_obj_center(cancel_btn_label);
    lv_obj_add_event_cb(cancel_btn, close_order_delete_confirm_modal_cb, LV_EVENT_CLICKED, NULL);
}

// 表格点击事件处理
static void orders_table_event_cb(lv_event_t *e) 
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_VALUE_CHANGED) 
    {
        uint16_t col, row;
        lv_table_get_selected_cell(target, &row, &col);
        
        if (row == 0) return; // 标题行
        
        // 找到对应的订单
        User *current_user = userList->head;
        int order_count = 0;
        int target_row = row;
        
        // 遍历用户和订单找到对应的订单
        while (current_user && target_row > 0) 
        {
            if (target_row <= current_user->orderCount) 
            {
                // 找到对应的订单
                int order_index = target_row - 1;
                
                if (col == 6) 
                { // 点击删除列
                    show_order_delete_confirm_dialog(current_user, order_index);
                }
                break;
            }
            else 
            {
                target_row -= current_user->orderCount;
                current_user = current_user->next;
            }
        }
    }
}

// 刷新按钮点击事件
static void refresh_orders_btn_event_cb(lv_event_t *e) 
{
    refresh_orders_table();
}

void init_user_management_tab3(lv_obj_t *parent) 
{
    // 创建容器
    lv_obj_t *container = lv_obj_create(parent);
    lv_obj_set_size(container, lv_pct(100), lv_pct(100));
    lv_obj_set_style_border_width(container, 0, 0);
    lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(container);
    lv_label_set_text(title, "订单管理");
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_LEFT, 20, 10);
    
    // 刷新按钮
    lv_obj_t *refresh_btn = lv_btn_create(container);
    lv_obj_set_size(refresh_btn, 100, 40);
    lv_obj_align(refresh_btn, LV_ALIGN_TOP_RIGHT, -20, 10);
    lv_obj_t *refresh_label = lv_label_create(refresh_btn);
    lv_label_set_text(refresh_label, "刷新");
    lv_obj_set_style_text_font(refresh_label, font_chinese, 0);
    lv_obj_center(refresh_label);
    lv_obj_add_event_cb(refresh_btn, refresh_orders_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 创建订单表格
    orders_table = lv_table_create(container);
    lv_obj_set_size(orders_table, lv_pct(95), lv_pct(80));
    lv_obj_align(orders_table, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_set_style_text_font(orders_table, font_chinese, 0);
    
    // 设置表格列宽
    lv_table_set_col_width(orders_table, 0, 100); // 用户名
    lv_table_set_col_width(orders_table, 1, 100); // 车次
    lv_table_set_col_width(orders_table, 2, 100); // 出发站
    lv_table_set_col_width(orders_table, 3, 100); // 到达站
    lv_table_set_col_width(orders_table, 4, 120); // 日期
    lv_table_set_col_width(orders_table, 5, 80);  // 座位号
    lv_table_set_col_width(orders_table, 6, 80);  // 操作
    
    // 设置表格标题
    lv_table_set_cell_value(orders_table, 0, 0, "用户名");
    lv_table_set_cell_value(orders_table, 0, 1, "车次");
    lv_table_set_cell_value(orders_table, 0, 2, "出发站");
    lv_table_set_cell_value(orders_table, 0, 3, "到达站");
    lv_table_set_cell_value(orders_table, 0, 4, "日期");
    lv_table_set_cell_value(orders_table, 0, 5, "座位号");
    lv_table_set_cell_value(orders_table, 0, 6, "操作");
    
    // 设置表格事件
    lv_obj_add_event_cb(orders_table, orders_table_event_cb, LV_EVENT_ALL, NULL);
    
    
    // 初始加载数据
    refresh_orders_table();
}